#include "ventilador.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsProxyWidget>

Ventilador::Ventilador()
{
    status = false;

    img_path = new QString(QApplication::applicationDirPath());

    ventilador_img = new QImage(QString("%1/Images/ventilador3.png").arg(*img_path));
    setRect(0, 0, ventilador_img->width(), ventilador_img->height());
    setFlag(ItemIsMovable);

    //float itemScale = (float) QApplication::desktop()->height() / (float)1080;
    //this->setScale(itemScale);
    this->setPos(800,50);

    horizontal_slider = new QSlider(Qt::Horizontal);
    horizontal_slider->setRange(200, 10000);
    horizontal_slider->setValue(2000);

    proxy = new QGraphicsProxyWidget;
    proxy->setWidget(horizontal_slider);
    proxy->setParentItem(this);
    proxy->setVisible(false);

    this->setTransformOriginPoint(this->boundingRect().center());

    anim = new QPropertyAnimation(this, "rotation");
    anim->setEasingCurve(QEasingCurve::Linear);
    anim->setDuration(500);
    anim->setStartValue(0);
    anim->setEndValue(-360);
    anim->setLoopCount(-1);

    proxy->setTransformOriginPoint(this->boundingRect().center());
    anim2 = new QPropertyAnimation(proxy, "rotation");
    anim2->setEasingCurve(QEasingCurve::Linear);
    anim2->setDuration(500);
    anim2->setStartValue(0);
    anim2->setEndValue(360);
    anim2->setLoopCount(-1);
}

void Ventilador::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->drawImage( option->exposedRect, *ventilador_img, option->exposedRect, Qt::OrderedAlphaDither );
}

void Ventilador::changeStatus()
{
    status = !status;

    if(status){
        anim->start();
        anim2->start();
    }
    else { anim->pause(); anim2->pause(); }
}

void Ventilador::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    changeStatus();

    if(event->button() == Qt::RightButton)
    {
        if(proxy->isVisible())
            proxy->setVisible(false);
        else proxy->setVisible(true);
    }
}
